package cmd

import (
	"context"
	"server/internal/controller/video"
	"server/internal/controller/webpage"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	// "server/internal/controller/hello"
)

func MiddlewareCORS(r *ghttp.Request) {
	r.Response.CORSDefault()
	r.Middleware.Next()
}

var Main = gcmd.Command{
	Name:  "main",
	Usage: "main",
	Brief: "start http server",
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
		s := g.Server()
		w := webpage.New()
		s.Group("/", func(group *ghttp.RouterGroup) {
			// group.Middleware(ghttp.MiddlewareHandlerResponse)
			group.Middleware(MiddlewareCORS)
			group.GET("/", w.Get)
			group.GET("/favicon.png", w.GetIcon)
			group.GET("/*any", w.GetDists)
		})
		// s.Group("/video", func(group *ghttp.RouterGroup) {
		// 	group.Middleware(ghttp.MiddlewareHandlerResponse)
		// 	group.Bind(
		// 		video.New(),
		// 	)
		// })
		videoRoute := video.New()
		s.Group("/video", func(group *ghttp.RouterGroup) {
			group.Middleware(MiddlewareCORS)
			group.GET("/caption", videoRoute.CaptionGet)
			group.GET("/metadata", videoRoute.MetadataGet)
			group.GET("/notes", videoRoute.NoteGet)
			group.GET("/poster", videoRoute.PosterGet)
			group.GET("/", videoRoute.VideoGet)
			group.POST("/", videoRoute.Post)
		})
		// s.BindObjectRest("/video", video.New())

		s.BindMiddlewareDefault(MiddlewareCORS)
		s.SetServerRoot("resource/public")
		s.Run()
		return nil
	},
}
